package pl.hucal.wmkassist.network;

import pl.hucal.wmkassist.R;
import pl.hucal.wmkassist.activities.LoggingableActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * Logging activity
 * 
 * Activity launches "Logging in with Facebook" tools and tries to authorize
 * user with his/her Facebook account
 * 
 * @author Tadeusz Hucal <tadeusz.hucal@gmail.com>
 */
public class LoggingActivity extends Activity implements LoggingableActivity
{
    /** Tag for LogCat */
    private static final String TAG = "WMK_LoggingActivity";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set view
        setContentView(R.layout.login);
        // Login button
        TextView loginButton = (TextView) findViewById(R.id.loginClickableArea);
        loginButton.setClickable(true);
        loginButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // Authorize with Facebook account
                Log.v(TAG, "Init Facebook connection");
                FacebookConnector.getInstance().connect(LoggingActivity.this);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        // Finish Facebook 'things'
        FacebookConnector.getInstance().authorizeCallback(requestCode, resultCode, data);
        // FIXME tutaj wczhodza okienka z facebook connector

        Log.d(TAG, "a1" + requestCode);
        Log.d(TAG, "a2" + resultCode);
        Log.d(TAG, "a3" + data.toString());
        Log.d(TAG, "Logging process finished");
        // Finish activity
        if (FacebookConnector.getInstance().isLogged())
        {
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onBackPressed()
    {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }

    @Override
    public void onLoggingError(String message)
    {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setMessage(getString(R.string.error_login_prefix) + message).setCancelable(false)
                .setTitle(R.string.error_header)
                .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                });
        AlertDialog errorDialog = dialogBuilder.create();
        errorDialog.show();
    }
}
